package org.dandelero.properties.framework;

import java.util.Arrays;
import java.util.List;

/**
 * A predicate that applies all of the specified predicates.
 * 
 * @author dandelero
 * 
 * @param <T>
 *            the type of data to be checked.
 */
public class AllPredicate<T> implements Predicate<T> {

	// ==================================================
	// Fields.
	// ==================================================

	/**
	 * All the predicates to be applied.
	 */
	private List<Predicate<T>> predicatesToApply;

	// ==================================================
	// Initialisation.
	// ==================================================

	/**
	 * Constructs a predicate that returns true when all of the specified predicates return true.
	 * 
	 * @param predicates
	 *            all the predicate to be apllied.
	 */
	public AllPredicate(Predicate<T>... predicates) {
		this.predicatesToApply = Arrays.asList(predicates);
	}

	// ==================================================
	// Predicate implementation.
	// ==================================================

	/**
	 * The check as to whether a property should be accepted or rejected.
	 * 
	 * @param o
	 *            the object.
	 * @return true or false.
	 */
	public boolean evaluate(T o) {
		for (Predicate<T> predicate : predicatesToApply) {
			if (!predicate.evaluate(o)) {
				return false;
			}
		}
		return true;
	}
}
